{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "%matplotlib inline\n",
    "!mkdir -p data/plots\n",
    "import jsonlines\n",
    "import sentencepiece as spm\n",
    "\n",
    "import pandas as pd\n",
    "import numpy as np\n",
    "\n",
    "import plotnine as p9\n",
    "\n",
    "sp = spm.SentencePieceProcessor()\n",
    "sp.Load('../data/codesearchnet_javascript/csnjs_8k_9995p_unigram_url.model')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "%%time\n",
    "data = []\n",
    "with jsonlines.open('../data/codesearchnet_javascript/javascript_train_supervised.jsonl') as f:\n",
    "    for obj in f:\n",
    "        data.append(obj)\n",
    "\n",
    "df = pd.DataFrame(data)\n",
    "df['func_name_len'] = df['func_name'].str.len()\n",
    "df['code_len'] = df['code'].str.len()\n",
    "df['func_name_subword_len'] = df['func_name'].apply(lambda x: len(sp.EncodeAsIds(x)))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "func_name_ids = {k: idx for idx, k in enumerate(set(df['func_name']))}\n",
    "df['func_name_idx'] = df['func_name'].apply(lambda x: func_name_ids[x])\n",
    "print(len(func_name_ids))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import matplotlib.pyplot as plt\n",
    "from collections import Counter\n",
    "x = Counter(df['func_name'])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "print(x)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "hot_tokens = list(idx for idx, value in x.items() if value > 1)\n",
    "plt.plot(list(reversed(sorted(x.values()))))\n",
    "len(hot_tokens)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "p9.ggplot(\n",
    "    data=df,\n",
    "    mapping=p9.aes(x='func_name_idx')\n",
    ") + p9.stat_ecdf() + p9.xlab('Identifier ID') + p9.ylab('CDF') + p9.theme_classic(16)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "plt_identifier_len = p9.ggplot(\n",
    "    data=df,\n",
    "    mapping=p9.aes(x='func_name_len')\n",
    ") + p9.stat_ecdf() + p9.xlab('Identifier length') + p9.ylab('CDF') + p9.theme_classic(16)\n",
    "plt_identifier_len.draw()\n",
    "plt_identifier_len.save('data/plots/identifier_length.pdf')\n",
    "\n",
    "plt_code_len = p9.ggplot(\n",
    "    data=df,\n",
    "    mapping=p9.aes(x='code_len')\n",
    ") + p9.stat_ecdf() + p9.xlab('Method body length') + p9.ylab('CDF') + p9.theme_classic(16) + p9.xlim(0, 10000)\n",
    "plt_code_len.draw()\n",
    "plt_code_len.save('data/plots/code_length.pdf')\n",
    "\n",
    "n_subwords = p9.ggplot(\n",
    "    data=df,\n",
    "    mapping=p9.aes(x='func_name_subword_len')\n",
    ") + p9.stat_ecdf() + p9.xlab('Identifier subword length') + p9.ylab('CDF')\n",
    "n_subwords.draw()\n",
    "n_subwords.save('data/plots/identifier_subword_length.pdf')"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
